<?php
	/*Gérald Plusquellec*/
	class controller_user
	{
		public $controller;
		public $action;
		
		public function __construct()
		{
			$this -> controller = controller :: getInstance();
            $this -> action = $action = getvar('**action**');
            $this -> $action();
            $this -> controller -> html();
		}
        
        public function add()
        {
            $form = form('user');
            
            if (count($_POST))
            {
                $valid = $form -> isValid($_POST);
                // $verifyCaptcha = verifyCaptcha();
                $verifToken = verifToken();
                
                if (true === $valid && true === $verifToken)
                {
                    $datas = $this -> datas();
                    insertDb('users', $datas);
                    setvar('__messageForm__', 'Ajour réussi');
                    setvar('**action**', 'liste');
                    $this -> liste();
                }
                else
                    $form -> populate($_POST);
            }
			
            $f = $form -> render(new Zend_View);
            $f = sanitForm($f);
            // $f = addCaptcha($f);
            
			setvar('__form__', $f);
            viewRenderer();
        }
        
        public function view()
        {
            $token = getvar('__token__');
            $id = getvar('__id__');
            viewRenderer();
        }
        
        public function edit()
        {
            $db = db();
            if (!count($_POST))
            {
                $token = getvar('__token__');
                $id = getvar('__id__');
            }
            else
            {
                $id = getvarpost('id');
                $tpken = getvarpost('tpken');
            }
            
            $form = form('user');
            hidden($form, 'id', $id);
            
            if (count($_POST))
            {
                $valid = $form -> isValid($_POST);
                // $verifyCaptcha = verifyCaptcha();
                $verifToken = verifToken();
                
                if (true === $valid && true === $verifToken)
                {
                    $datas = $this -> datas();
                    updateDbId('users', $datas, $id);
                    setvar('__messageForm__', 'Mise à jour réussie');
                    setvar('**action**', 'liste');
                    $this -> liste();
                }
                else
                    $form -> populate($_POST);
            }
            else
            {
                $q = "SELECT * FROM users WHERE id = $id";
                $data = $db -> fetchArray_one($q);
                if (count($data))
                    $form -> populate($data);
            }
			
            $f = $form -> render(new Zend_View);
            $f = sanitForm($f);
            // $f = addCaptcha($f);
            
			setvar('__form__', $f);
            viewRenderer();
        }
        
        public function dup()
        {
            $db = db();
            if (!count($_POST))
            {
                $token = getvar('__token__');
                $id = getvar('__id__');
            }
            else
            {
                $id = getvarpost('id');
                $tpken = getvarpost('tpken');
            }
            
            $form = form('user');
            
            if (count($_POST))
            {
                $valid = $form -> isValid($_POST);
                // $verifyCaptcha = verifyCaptcha();
                $verifToken = verifToken();
                
                if (true === $valid && true === $verifToken)
                {
                    $datas = $this -> datas();
                    insertDb('users', $datas);
                    setvar('__messageForm__', 'Duplication réussie');
                    setvar('**action**', 'liste');
                    $this -> liste();
                }
                else
                    $form -> populate($_POST);
            }
            else
            {
                $q = "SELECT * FROM users WHERE id = $id";
                $data = $db -> fetchArray_one($q);
                if (count($data))
                    $form -> populate($data);
            }
			
            $f = $form -> render(new Zend_View);
            $f = sanitForm($f);
            // $f = addCaptcha($f);
            
			setvar('__form__', $f);
            viewRenderer();
        }
        
        public function liste()
        {
            $db = db();
            $limit = config('limit');
            $page = getvar('**page**');
            $action = getvar('**action**');
            
            $filter = getvar('__filter__');
            $nofilter = getvar('__nofilter__');
            $export = getvar('__export__');
            
            if ($nofilter == 1)
                setoption('__filterSQL_' . $page . '__', '1 = 1');
            if ($filter == 1)
            {
                extract($_POST);
                
                $filterFields = isset($_POST['filterFields']) ? $_POST['filterFields'] : null;
                $filterOperators = isset($_POST['filterOperators']) ? $_POST['filterOperators'] : null;
                $filterValues = isset($_POST['filterValues']) ? $_POST['filterValues'] : null;
                
                if (null != $filterFields && null != $filterOperators && null != $filterValues)
                {
                    $filter = '';
                    
                    $filterFieldsTab = explode(',', $filterFields);
                    $filterOperatorsTab = explode(',', $filterOperators);
                    $filterValuesTab = explode(',', $filterValues);
                    
                    $i = 0;
                    foreach ($filterFieldsTab as $field)
                    {
                        $operator = $filterOperatorsTab[$i];
                        $value = $filterValuesTab[$i];
                        
                        switch ($field)
                        {
                            case 'role':
                                $fieldSQL = 'roles.nom';
                                break;
                            case 'langue':
                                $fieldSQL = 'users.langue';
                                break;
                            case 'login':
                                $fieldSQL = 'users.login';
                                break;
                            case 'nom':
                                $fieldSQL = 'users.nom';
                                break;
                            case 'prenom':
                                $fieldSQL = 'users.prenom';
                                break;
                            case 'courriel':
                                $fieldSQL = 'users.courriel';
                                break;
                        }
                        
                        if ($operator <> 'LIKE' && $operator <> 'NOT LIKE')
                            $filter .= "$fieldSQL $operator '" . addslashes($value) . "' && ";
                        else
                            $filter .= "$fieldSQL $operator '%" . addslashes($value) . "%' && ";
                        $i++;
                    }
                    
                    $filter = substr($filter, 0, -4);
                    setoption('__filterSQL_' . $page . '__', $filter);
                }
                
            }
            
            $filterSQL = getoption('__filterSQL_' . $page . '__');
            
            if (null === $filterSQL)
            {
                $filterSQL = '1 = 1';
                setoption('__filterSQL_' . $page . '__', $filterSQL);
            }
            
            $filterFields = array(
                'langue' => __('Langue'),
                'role' => __('Rôle'),
                'login' => __('Login'),
                'nom' => __('Nom'),
                'prenom' => __('Prénom'),
                'courriel' => __('Courriel')
            );
            
            list($numPage, $order, $sens, $start) = optionsPage('users.nom');
            $options = array(
                'numPage_' . $page => $numPage,
                'order_' . $page => $order,
                'sens_' . $page => $sens,
                'start_' . $page => $start,
                'filter' => 1
            );
        
            $filterUrl = makeUrl($page, $action, $options);
            
            $options = array(
                'numPage_' . $page => $numPage,
                'order_' . $page => $order,
                'sens_' . $page => $sens,
                'start_' . $page => $start,
                'nofilter' => 1
            );
        
            $nofilterUrl = makeUrl($page, $action, $options);
            
            $options = array(
                'numPage_' . $page => $numPage,
                'order_' . $page => $order,
                'sens_' . $page => $sens,
                'start_' . $page => $start,
                'export' => 1
            );
        
            $exportUrl = makeUrl($page, $action, $options);
            
            
            $q = "SELECT 
            users.id,
            users.langue,
            roles.nom as role,
            users.login,
            users.nom,
            users.prenom,
            users.courriel
            FROM users 
            LEFT JOIN roles ON users.role = roles.id
            WHERE $filterSQL";
            
            $resTotal = $db -> fetchArray($q);
            
            $total = count($resTotal);
            if ($total > $limit)
                $pagination = pagination($numPage, $total);
            else
                $pagination = null;
            
            if ($export == 1)
                makeExcel($resTotal, 'utilisateurs');
            
            $q = "SELECT 
            users.id,
            users.langue,
            roles.nom as role,
            users.login,
            users.nom,
            users.prenom,
            users.courriel
            FROM users 
            LEFT JOIN roles ON users.role = roles.id
            WHERE $filterSQL
            ORDER BY $order $sens LIMIT $start, $limit";
            
            $data = $db -> fetchArray($q);
            
            setvar('numPage', $numPage);
			setvar('start', $start);
			setvar('order', $order);
			setvar('sens', $sens);
			setvar('data', $data);
			setvar('pagination', $pagination);
			setvar('total', $total);
			setvar('filterUrl', $filterUrl);
			setvar('exportUrl', $exportUrl);
			setvar('nofilterUrl', $nofilterUrl);
			setvar('filterFields', $filterFields);
			setvar('filterSQL', $filterSQL);
			setvar('startPage', (($limit * $numPage) - ($limit - 1)));
			setvar('endPage', ($limit * $numPage > $total) ? $total : $limit * $numPage);
            
            viewRenderer();
        }
        
        function datas()
        {
            extract($_POST);
            $datas = array(
                'nom' => $nom,
                'prenom' => $prenom,
                'actif' => $actif,
                'role' => $role,
                'langue' => $langue,
                'login' => $login,
                'mdp' => md5($mdp),
                'courriel' => $courriel
            );
            return $datas;
        }
	}
?>